package com.ihighest.app.infox.adapter;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ihighest.app.infox.R;
import com.ihighest.app.infox.activity.InformationalContentActivity;
import com.ihighest.app.infox.myview.PagerAdapter;

public class RealtimeHotNewsAdapter extends PagerAdapter {

	private JSONArray array;

	private final Context context;

	public RealtimeHotNewsAdapter(Context context, JSONArray array) {
		this.context = context;
		this.array = array;
	}

	@Override
	public int getCount() {
		return array != null ? array.length() : 0;
	}

	public void setDatas(JSONArray array) {
		this.array = array;
		this.notifyDataSetChanged();
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == object;
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {

		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View view = inflater.inflate(R.layout.infox_item_realtime_hotnews_list, null); // 一行文字的layout

		TextView leftTextView = (TextView) view.findViewById(R.id.information_type);

		TextView rightTextView = (TextView) view.findViewById(R.id.information_RealTimeHotSpots);

		try {
			leftTextView.setText("【实时热点】");
			rightTextView.setText(array.getJSONObject(position).getString("title"));
			view.setTag(array.getJSONObject(position).getString("id"));

		} catch (JSONException e) {
			e.printStackTrace();
		}
		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent detailIntent = new Intent(context, InformationalContentActivity.class);
				detailIntent.putExtra("newsId", arg0.getTag().toString());
				context.startActivity(detailIntent);
			}
		});
		container.addView(view, 0);

		return view;

	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {

		container.removeView((View) object);

	}
}
